package com.chat.view;

import com.chat.client.ClientChat;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;

//注册页面
public class registerView extends JFrame {

    private ClientChat client = null;
    private JTextField UText = null;
    private JTextField MText = null;
    private JTextField PText = null;
    private JTextField ZText = null;

    public registerView(String server){
        String[] split = server.split(":");
        this.client = new ClientChat(split[0], Integer.parseInt(split[1]));
    }

    public void init(){

        this.setTitle("聊天客户端");
        //设置窗口大小和初始位置
        this.setBounds(700,400,280,240);
        //设置窗口不可改变
        this.setResizable(false);
        //设置窗口关闭按钮点击后关闭窗口
        this.setDefaultCloseOperation(3);

        JPanel panel = new JPanel();
        //设置布局模式为绝对布局
        panel.setLayout(null);

        //用户名
        JLabel ULabel = new JLabel("用户名：");
        ULabel.setBounds(37,15,60,30);
        panel.add(ULabel);
        UText = new JTextField();
        UText.setBounds(90,15,150,30);
        panel.add(UText);

        //密码
        JLabel MLabel = new JLabel("密码：");
        MLabel.setBounds(50,55,60,30);
        panel.add(MLabel);
        MText = new JPasswordField();
        MText.setBounds(90,55,150,30);
        MText.setColumns(16);
        MText.setText("123456");
        panel.add(MText);

        //密码标签
        JLabel PLabel = new JLabel("确认密码：");
        PLabel.setBounds(23,95,80,30);
        panel.add(PLabel);
        PText = new JPasswordField();
        PText.setBounds(90,95,150,30);
        PText.setColumns(16);
        PText.setText("123456");
        panel.add(PText);

        //邀请码
        JLabel ZLabel = new JLabel("邀请码：");
        ZLabel.setBounds(37,135,60,30);
        panel.add(ZLabel);
        ZText = new JTextField();
        ZText.setBounds(90,135,150,30);
        ZText.setText("123456");
        panel.add(ZText);

        JButton register = new JButton("注册");
        register.setBounds(50,175,80,30);
        panel.add(register);

        JButton ret = new JButton("返回");
        ret.setBounds(160,175,80,30);
        panel.add(ret);

        UText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==10){
                    MText.requestFocus();
                }
            }
        });

        MText.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e){
                //当按下Enter键时，插入一个换行符(Enter键的keycode为10)
                if(e.getKeyCode()==10){
                    PText.requestFocus();
                }
            }

        });

        PText.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e){
                //当按下Enter键时，插入一个换行符(Enter键的keycode为10)
                if(e.getKeyCode()==10){

                    try {
//                    JOptionPane.showMessageDialog(new JPanel(), "提示消息", "标题",JOptionPane.WARNING_MESSAGE);
                        registerin();

                    } catch (IOException | ClassNotFoundException ioException) {
                        ioException.printStackTrace();
                        JOptionPane.showMessageDialog(new JPanel(), "连接服务失败。", "错误",JOptionPane.WARNING_MESSAGE);
                    }

                }
            }

        });

        //注册
        register.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try {
//                    JOptionPane.showMessageDialog(new JPanel(), "提示消息", "标题",JOptionPane.WARNING_MESSAGE);
                    registerin();

                } catch (IOException | ClassNotFoundException ioException) {
                    ioException.printStackTrace();
                    JOptionPane.showMessageDialog(new JPanel(), "连接服务失败。", "错误",JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        ret.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                new loginView().init();
                registerView.this.setVisible(false);

            }
        });



        this.add(panel);
        this.setVisible(true);
    }

    private void registerin() throws IOException, ClassNotFoundException{
        String user = UText.getText().trim();
        String pass = MText.getText().trim();
        String rpass = PText.getText().trim();
        String zcm = ZText.getText().trim();

        if (user.equals("") || user.matches("/\\w\\u4e00-\\u9fa5/") || user.length() > 16)
        {
            JOptionPane.showMessageDialog(new JPanel(), "用户名格式错误。", "错误",JOptionPane.WARNING_MESSAGE);
            UText.setText("");
            return;
        }

        if (pass.equals("") || !pass.matches("^\\w+$") || user.length() > 16)
        {
            JOptionPane.showMessageDialog(new JPanel(), "密码格式错误。", "错误",JOptionPane.WARNING_MESSAGE);
            MText.setText("");
            return;
        }

        if (!rpass.equals(pass))
        {
            JOptionPane.showMessageDialog(new JPanel(), "两次密码不一致。", "错误",JOptionPane.WARNING_MESSAGE);
            PText.setText("");
            return;
        }

        if (pass.equals("") || !zcm.matches("^\\w+$") || user.length() > 10)
        {
            JOptionPane.showMessageDialog(new JPanel(), "注册码格式错误", "错误",JOptionPane.WARNING_MESSAGE);
            ZText.setText("");
            return;
        }

        String news = client.putNews("register", user + "\t" + pass + "\t" + zcm);

        System.out.println(news);

        switch (news.trim())
        {
            case "exist":
                JOptionPane.showMessageDialog(new JPanel(), "用户已经存在", "提示",JOptionPane.WARNING_MESSAGE);
                break;
            case "fail":
                JOptionPane.showMessageDialog(new JPanel(), "注册失败。", "错误",JOptionPane.WARNING_MESSAGE);
                break;
            default:
                JOptionPane.showMessageDialog(new JPanel(), "注册成功。", "错误",JOptionPane.WARNING_MESSAGE);
                client.setUser(user);
                client.setId(Integer.parseInt(news.trim()));
                new ClientView(client).start();
                registerView.this.setVisible(false);
                break;
        }
    }

}
